/*
 * 
 * Segmented PictureReader.java
 * David Ing
 * 
 * Reads segmented images from Image team.  Finds all the different
 * colors and adds them into an array list.
 * 
 * Gives each of the different colors a name.
 * 
 * The program accepts an x and a y location and prints out the color
 * are that x y location
 * 
 * Plays associated sound files
 * ----------------------------------------------------------------------------
 */

import java.awt.Color;
import java.util.ArrayList;
import java.util.Scanner;

public class SegmentedPictureReader {
    public static final int DEAD_SPACE = 3; // seconds of deadspace played
    private int numColors;                  // number of colors in the picture
    static Picture pic;                     // segmented picture chosen by user
    static ArrayList<Color> colors;         // list of the different colors
	
/*-------------------------------------------------------------------------
 * SegmentedPictureReader
 * 
 * default constructor for Segmented Picture Reader
 * sets all fields to 0
 * 
 -------------------------------------------------------------------------*/
    public SegmentedPictureReader(){
        //  initializes number of colors
        numColors = 0;
        // creates new array for colors with initial capacity 0
        colors = new ArrayList<Color>(0);
    }


/*-------------------------------------------------------------------------
 * ReadPic(Picture pic)
 * 
 * Takes in segmented picture from user and assigns it to pic
 * calls findcolor to get an arrayList of all the different colors.
 * 
 * Returns true if successful, false if failure
 * 
 -------------------------------------------------------------------------*/
	public boolean readPic(Picture pic){
		// returns false if the picture does not exist
		if(pic.getFileName() == null){
			System.err.println("PICTURE DOES NOT EXIST!!!");
			return false;
		}
		
		// call find colors to find all the different colors in this picture
		colors = findColors(pic);
		
		return true;
	}
	
/*-------------------------------------------------------------------------
 * findColors(Picture pic)
 * 
 * Takes in a picture, finds all the different colors while incrementing
 * the color count.  Returns an ArrayList of colors.
 -------------------------------------------------------------------------*/
	public ArrayList<Color> findColors(Picture pic){
		// create array of all the pixels in the picture
		Pixel[] pixels = pic.getPixels();
		
		// loops through all the pixels
		for(int i = 0; i < pixels.length; i++){
			// checks if current pixel's color is already in the color array
			if(!colors.contains( pixels[i].getColor() )){
				colors.add(pixels[i].getColor());
				numColors++;
			}
		}
		
		return colors;
	}
	
/*-------------------------------------------------------------------------
 * playSoundRegion(Color ccc, String pictureName)
 * 
 * Takes in a color and plays the appropriate sound from the file according
 * to the given color region.  Sound file should be IN THE SAME FILE AS
 * THE PICTURE and in the following format:
 * 
 * FORMAT:  <pictureName>.<RED>.<GREEN>.<BLUE>.wav
 * 
 * No return value
 * 
 --------------------------------------------------------------------------*/
	public static void playSoundRegion(Color ccc, String pictureName) {
		// make a sound file to play, default sound is dead silence
		Sound soundFile = new Sound(DEAD_SPACE);
		
		// if the color is not black, load sound file
		if(!(ccc.getRed() == 0 && ccc.getGreen() == 0 && ccc.getBlue() == 0)){
			// get appropriate sound file according to format
			try {
				soundFile.loadFromFile(pictureName + "." + ccc.getRed()+ "." + 
								 ccc.getGreen()+ "." + ccc.getBlue() + ".wav");
		
			// prints error if file not found
			} catch (SoundException e) {
				System.err.println("ERROR: SOUND FILE DOES NOT EXIST!!!");
			}
		}
		// play sound file
		soundFile.play();
	}
	
	
	
/*-------------------------------------------------------------------------
 * main
 * 
 * Main method does the following
 * 1.  Takes in picture file
 * 2.  Reads the segmented picture
 * 3.  accepts x y input and will play appropriate sound file for color
 * 
 -------------------------------------------------------------------------*/
	public static void main(String[] args) throws SoundException{
		// color of the pixel that the user chooses
		Color ccc;
		// x and y inputs for the picture from user
		int x,y;
		// Scanner to read input from user
		Scanner scan = new Scanner(System.in);
		
		// Creates default SegmentedPictureReader to read the segmented image
		SegmentedPictureReader seg = new SegmentedPictureReader();
		
		// user chooses segmented picture to process
		pic = new Picture(FileChooser.pickAFile());
		
		// processes the image data
		if(seg.readPic(pic))
		{
			pic.explore();
			// gets the pixels for the picture
			Pixel[] pixels = pic.getPixels();   
		
			// prompt to check xy
			System.out.print("Do you want to check a point? ");
			System.out.println("Any character to continue, EOF to quit)");
			while(scan.hasNext()){
				// throw away the buffer 
				scan.next();
				
				// asks for x/y input
				System.out.print("Enter in some x value:  ");
				x = scan.nextInt();
				System.out.print("Enter in some y value:  ");
				y = scan.nextInt();

				// asign color of the chosen pixel
				ccc = pixels[x + y*pic.getWidth()].getColor();
				// prints out color region
				System.out.println(ccc);
				// plays sound from color region
				playSoundRegion(ccc, pic.getFileName());
			
				// asks to continue
				System.out.print("Do you want to check a point? ");
				System.out.println("Any character to continue, EOF to quit)");
			}
		}
		// closes the java application
		System.exit(0);
	}
}
